<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Comment_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 根据商品id得到平均分
	 */
	public function get_avg_score($id){
		$this->db->select_avg('score');
		$this->db->where('productid',$id);
		$this->db->where('score!=0');
		$query = $this->db->get('t_comment');
		return $query->row_array();
	}
	/*
	 * 根据商品ID和分页信息得到商品评价信息
	 */
	public function find_by_product($productid,$page,$offset){
		$this->db->where("productid", $productid);
		$this->db->order_by("date", "desc");
		$this->db->limit($page, $offset);
		$query = $this->db->get('t_comment');
		return $query->result_array();
	}
	/*
	 * 保存商品评价信息
	 */
	public function create($comment){
		$this->db->insert('t_comment', $comment);
		return $this->db->insert_id();
	}
}